home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / MacPerl 506 appl folder.sit / MacPerl 506 appl folder / Mac_Perl_506r1m_appl / lib / ftp.pl < prev    next >
Text File  |  1995-03-20  |  24KB  |  1,079 lines

  1. #-*-perl-*-
  2. # This is a wrapper to the chat2.pl routines that make life easier
  3. # to do ftp type work.
  4. # Mostly by Lee McLoughlin <lmjm@doc.ic.ac.uk>
  5. # based on original version by Alan R. Martello <al@ee.pitt.edu>
  6. # And by A.Macpherson@bnr.co.uk for multi-homed hosts
  7. #
  8. # $Header: /a/swan/home/swan/staff/csg/lmjm/src/perl/mirror/RCS/ftp.pl,v 1.17 1993/04/21 10:06:54 lmjm Exp lmjm $
  9. # $Log: ftp.pl,v $
  10. # Revision 1.17  1993/04/21  10:06:54  lmjm
  11. # Send all status reports to STDERR not to STDOUT (to allow use by ftpcat).
  12. # Allow target file to be '-' meaning STDOUT
  13. # Added ftp'quote
  14. #
  15. # Revision 1.16  1993/01/28  18:59:05  lmjm
  16. # Allow socket arguemtns to come from main.
  17. # Minor cleanups - removed old comments.
  18. #
  19. # Revision 1.15  1992/11/25  21:09:30  lmjm
  20. # Added another REST return code.
  21. #
  22. # Revision 1.14  1992/08/12  14:33:42  lmjm
  23. # Fail ftp'write if out of space.
  24. #
  25. # Revision 1.13  1992/03/20  21:01:03  lmjm
  26. # Added in the proxy ftp code from Edwards Reed <err@cinops.xerox.com>
  27. # Added  ftp'delete from Aaron Wohl <aw0g+@andrew.cmu.edu>
  28. #
  29. # Revision 1.12  1992/02/06  23:25:56  lmjm
  30. # Moved code around so can use this as a lib for both mirror and ftpmail.
  31. # Time out opens.  In case Unix doesn't bother to.
  32. #
  33. # Revision 1.11  1991/11/27  22:05:57  lmjm
  34. # Match the response code number at the start of a line allowing
  35. # for any leading junk.
  36. #
  37. # Revision 1.10  1991/10/23  22:42:20  lmjm
  38. # Added better timeout code.
  39. # Tried to optimise file transfer
  40. # Moved open/close code to not leak file handles.
  41. # Cleaned up the alarm code.
  42. # Added $fatalerror to show wether the ftp link is really dead.
  43. #
  44. # Revision 1.9  1991/10/07  18:30:35  lmjm
  45. # Made the timeout-read code work.
  46. # Added restarting file gets.
  47. # Be more verbose if ever have to call die.
  48. #
  49. # Revision 1.8  1991/09/17  22:53:16  lmjm
  50. # Spot when open_data_socket fails and return a failure rather than dying.
  51. #
  52. # Revision 1.7  1991/09/12  22:40:25  lmjm
  53. # Added Andrew Macpherson's patches for hosts without ip forwarding.
  54. #
  55. # Revision 1.6  1991/09/06  19:53:52  lmjm
  56. # Relaid out the code the way I like it!
  57. # Changed the debuggin to produce more "appropriate" messages
  58. # Fixed bugs in the ordering of put and dir listing.
  59. # Allow for hash printing when getting files (a la ftp).
  60. # Added the new commands from Al.
  61. # Don't print passwords in debugging.
  62. #
  63. # Revision 1.5  1991/08/29  16:23:49  lmjm
  64. # Timeout reads from the remote ftp server.
  65. # No longer call die expect on fatal errors.  Just return fail codes.
  66. # Changed returns so higher up routines can tell whats happening.
  67. # Get expect/accept in correct order for dir listing.
  68. # When ftp_show is set then print hashes every 1k transfered (like ftp).
  69. # Allow for stripping returns out of incoming data.
  70. # Save last error in a global string.
  71. #
  72. # Revision 1.4  1991/08/14  21:04:58  lmjm
  73. # ftp'get now copes with ungetable files.
  74. # ftp'expect code changed such that the string_to_print is
  75. # ignored and the string sent back from the remote system is printed
  76. # instead.
  77. # Implemented patches from al.  Removed spuiours tracing statements.
  78. #
  79. # Revision 1.3  1991/08/09  21:32:18  lmjm
  80. # Allow for another ok code on cwd's
  81. # Rejigger the log levels
  82. # Send ¥r¥n for some odd ftp daemons
  83. #
  84. # Revision 1.2  1991/08/09  18:07:37  lmjm
  85. # Don't print messages unless ftp_show says to.
  86. #
  87. # Revision 1.1  1991/08/08  20:31:00  lmjm
  88. # Initial revision
  89. #
  90.  
  91. die "ftp.pl not (yet) implemented on Mac";
  92.  
  93. require 'chat2.pl';
  94. require 'socket.ph';
  95.  
  96.  
  97. package ftp;
  98.  
  99. if( defined( &main'PF_INET ) ){
  100.     $pf_inet = &main'PF_INET;
  101.     $sock_stream = &main'SOCK_STREAM;
  102.     local($name, $aliases, $proto) = getprotobyname( 'tcp' );
  103.     $tcp_proto = $proto;
  104. }
  105. else {
  106.     # XXX hardwired $PF_INET, $SOCK_STREAM, 'tcp'
  107.     # but who the heck would change these anyway? (:-)
  108.     $pf_inet = 2;
  109.     $sock_stream = 1;
  110.     $tcp_proto = 6;
  111. }
  112.  
  113. # If the remote ftp daemon doesn't respond within this time presume its dead
  114. # or something.
  115. $timeout = 30;
  116.  
  117. # Timeout a read if I don't get data back within this many seconds
  118. $timeout_read = 20 * $timeout;
  119.  
  120. # Timeout an open
  121. $timeout_open = $timeout;
  122.  
  123. # This is a "global" it contains the last response from the remote ftp server
  124. # for use in error messages
  125. $ftp'response = "";
  126. # Also ftp'NS is the socket containing the data coming in from the remote ls
  127. # command.
  128.  
  129. # The size of block to be read or written when talking to the remote
  130. # ftp server
  131. $ftp'ftpbufsize = 4096;
  132.  
  133. # How often to print a hash out, when debugging
  134. $ftp'hashevery = 1024;
  135. # Output a newline after this many hashes to prevent outputing very long lines
  136. $ftp'hashnl = 70;
  137.  
  138. # If a proxy connection then who am I really talking to?
  139. $real_site = "";
  140.  
  141. # This is just a tracing aid.
  142. $ftp_show = 0;
  143. sub ftp'debug
  144. {
  145.     $ftp_show = @_[0];
  146. #    if( $ftp_show ){
  147. #        print STDERR "ftp debugging on¥n";
  148. #    }
  149. }
  150.  
  151. sub ftp'set_timeout
  152. {
  153.     $timeout = @_[0];
  154.     $timeout_open = $timeout;
  155.     $timeout_read = 20 * $timeout;
  156.     if( $ftp_show ){
  157.         print STDERR "ftp timeout set to $timeout¥n";
  158.     }
  159. }
  160.  
  161.  
  162. sub ftp'open_alarm
  163. {
  164.     die "timeout: open";
  165. }
  166.  
  167. sub ftp'timed_open
  168. {
  169.     local( $site, $ftp_port, $retry_call, $attempts ) = @_;
  170.     local( $connect_site, $connect_port );
  171.     local( $res );
  172.  
  173.     alarm( $timeout_open );
  174.  
  175.     while( $attempts-- ){
  176.         if( $ftp_show ){
  177.             print STDERR "proxy connecting via $proxy_gateway [$proxy_ftp_port]¥n" if $proxy;
  178.             print STDERR "Connecting to $site";
  179.             if( $ftp_port != 21 ){
  180.                 print STDERR " [port $ftp_port]";
  181.             }
  182.             print STDERR "¥n";
  183.         }
  184.         
  185.         if( $proxy ) {
  186.             if( ! $proxy_gateway ) {
  187.                 # if not otherwise set
  188.                 $proxy_gateway = "internet-gateway";
  189.             }
  190.             if( $debug ) {
  191.                 print STDERR "using proxy services of $proxy_gateway, ";
  192.                 print STDERR "at $proxy_ftp_port¥n";
  193.             }
  194.             $connect_site = $proxy_gateway;
  195.             $connect_port = $proxy_ftp_port;
  196.             $real_site = $site;
  197.         }
  198.         else {
  199.             $connect_site = $site;
  200.             $connect_port = $ftp_port;
  201.         }
  202.         if( ! &chat'open_port( $connect_site, $connect_port ) ){
  203.             if( $retry_call ){
  204.                 print STDERR "Failed to connect¥n" if $ftp_show;
  205.                 next;
  206.             }
  207.             else {
  208.                 print STDERR "proxy connection failed " if $proxy;
  209.                 print STDERR "Cannot open ftp to $connect_site¥n" if $ftp_show;
  210.                 return 0;
  211.             }
  212.         }
  213.         $res = &ftp'expect( $timeout,
  214.                     120, "service unavailable to $site", 0, 
  215.                                 220, "ready for login to $site", 1,
  216.                     421, "service unavailable to $site, closing connection", 0);
  217.         if( ! $res ){
  218.             &chat'close();
  219.             next;
  220.         }
  221.         return 1;
  222.     }
  223.     continue {
  224.         print STDERR "Pausing between retries¥n";
  225.         sleep( $retry_pause );
  226.     }
  227.     return 0;
  228. }
  229.  
  230. sub ftp'open
  231. {
  232.     local( $site, $ftp_port, $retry_call, $attempts ) = @_;
  233.  
  234.     $SIG{ 'ALRM' } = "ftp¥'open_alarm";
  235.  
  236.     local( $ret ) = eval "&timed_open( '$site', $ftp_port, $retry_call, $attempts )";
  237.     alarm( 0 );
  238.  
  239.     if( $@ =~ /^timeout/ ){
  240.         return -1;
  241.     }
  242.     return $ret;
  243. }
  244.  
  245. sub ftp'login
  246. {
  247.     local( $remote_user, $remote_password ) = @_;
  248.  
  249.     if( $proxy ){
  250.         &ftp'send( "USER $remote_user@$site" );
  251.     }
  252.     else {
  253.         &ftp'send( "USER $remote_user" );
  254.     }
  255.         local( $val ) =
  256.                &ftp'expect($timeout,
  257.                230, "$remote_user logged in", 1,
  258.            331, "send password for $remote_user", 2,
  259.  
  260.            500, "syntax error", 0,
  261.            501, "syntax error", 0,
  262.            530, "not logged in", 0,
  263.            332, "account for login not supported", 0,
  264.  
  265.            421, "service unavailable, closing connection", 0);
  266.     if( $val == 1 ){
  267.         return 1;
  268.     }
  269.     if( $val == 2 ){
  270.         # A password is needed
  271.         &ftp'send( "PASS $remote_password" );
  272.  
  273.         $val = &ftp'expect( $timeout,
  274.            230, "$remote_user logged in", 1,
  275.  
  276.            202, "command not implemented", 0,
  277.            332, "account for login not supported", 0,
  278.  
  279.            530, "not logged in", 0,
  280.            500, "syntax error", 0,
  281.            501, "syntax error", 0,
  282.            503, "bad sequence of commands", 0, 
  283.  
  284.            421, "service unavailable, closing connection", 0);
  285.         if( $val == 1){
  286.             # Logged in
  287.             return 1;
  288.         }
  289.     }
  290.     # If I got here I failed to login
  291.     return 0;
  292. }
  293.  
  294. sub ftp'close
  295. {
  296.     &ftp'quit();
  297.     &chat'close();
  298. }
  299.  
  300. # Change directory
  301. # return 1 if successful
  302. # 0 on a failure
  303. sub ftp'cwd
  304. {
  305.     local( $dir ) = @_;
  306.  
  307.     &ftp'send( "CWD $dir" );
  308.  
  309.     return &ftp'expect( $timeout,
  310.         200, "working directory = $dir", 1,
  311.         250, "working directory = $dir", 1,
  312.  
  313.         500, "syntax error", 0,
  314.         501, "syntax error", 0,
  315.                 502, "command not implemented", 0,
  316.         530, "not logged in", 0,
  317.                 550, "cannot change directory", 0,
  318.         421, "service unavailable, closing connection", 0 );
  319. }
  320.  
  321. # Get a full directory listing:
  322. # &ftp'dir( remote LIST options )
  323. # Start a list goin with the given options.
  324. # Presuming that the remote deamon uses the ls command to generate the
  325. # data to send back then then you can send it some extra options (eg: -lRa)
  326. # return 1 if sucessful and 0 on a failure
  327. sub ftp'dir_open
  328. {
  329.     local( $options ) = @_;
  330.     local( $ret );
  331.     
  332.     if( ! &ftp'open_data_socket() ){
  333.         return 0;
  334.     }
  335.     
  336.     if( $options ){
  337.         &ftp'send( "LIST $options" );
  338.     }
  339.     else {
  340.         &ftp'send( "LIST" );
  341.     }
  342.     
  343.     $ret = &ftp'expect( $timeout,
  344.         150, "reading directory", 1,
  345.     
  346.         125, "data connection already open?", 0,
  347.     
  348.         450, "file unavailable", 0,
  349.         500, "syntax error", 0,
  350.         501, "syntax error", 0,
  351.         502, "command not implemented", 0,
  352.         530, "not logged in", 0,
  353.     
  354.            421, "service unavailable, closing connection", 0 );
  355.     if( ! $ret ){
  356.         &ftp'close_data_socket;
  357.         return 0;
  358.     }
  359.     
  360.     # 
  361.     # the data should be coming at us now
  362.     #
  363.     
  364.     # now accept
  365.     accept(NS,S) || die "accept failed $!";
  366.     
  367.     return 1;
  368. }
  369.  
  370.  
  371. # Close down reading the result of a remote ls command
  372. # return 1 if successful and 0 on failure
  373. sub ftp'dir_close
  374. {
  375.     local( $ret );
  376.  
  377.     # read the close
  378.     #
  379.     $ret = &ftp'expect($timeout,
  380.             226, "", 1,     # transfer complete, closing connection
  381.             250, "", 1,     # action completed
  382.  
  383.             425, "can't open data connection", 0,
  384.             426, "connection closed, transfer aborted", 0,
  385.             451, "action aborted, local error", 0,
  386.             421, "service unavailable, closing connection", 0);
  387.  
  388.     # shut down our end of the socket
  389.     &ftp'close_data_socket;
  390.  
  391.     if( ! $ret ){
  392.         return 0;
  393.     }
  394.  
  395.     return 1;
  396. }
  397.  
  398. # Quit from the remote ftp server
  399. # return 1 if successful and 0 on failure
  400. sub ftp'quit
  401. {
  402.     $site_command_check = 0;
  403.     @site_command_list = ();
  404.  
  405.     &ftp'send("QUIT");
  406.  
  407.     return &ftp'expect($timeout, 
  408.         221, "Goodbye", 1,     # transfer complete, closing connection
  409.     
  410.         500, "error quitting??", 0);
  411. }
  412.  
  413. sub ftp'read_alarm
  414. {
  415.     die "timeout: read";
  416. }
  417.  
  418. sub ftp'timed_read
  419. {
  420.     alarm( $timeout_read );
  421.     return sysread( NS, $buf, $ftpbufsize );
  422. }
  423.  
  424. sub ftp'read
  425. {
  426.     $SIG{ 'ALRM' } = "ftp¥'read_alarm";
  427.  
  428.     local( $ret ) = eval '&timed_read()';
  429.     alarm( 0 );
  430.  
  431.     if( $@ =~ /^timeout/ ){
  432.         return -1;
  433.     }
  434.     return $ret;
  435. }
  436.  
  437. # Get a remote file back into a local file.
  438. # If no loc_fname passed then uses rem_fname.
  439. # returns 1 on success and 0 on failure
  440. sub ftp'get
  441. {
  442.     local($rem_fname, $loc_fname, $restart ) = @_;
  443.     
  444.     if ($loc_fname eq "") {
  445.         $loc_fname = $rem_fname;
  446.     }
  447.     
  448.     if( ! &ftp'open_data_socket() ){
  449.         print STDERR "Cannot open data socket¥n";
  450.         return 0;
  451.     }
  452.  
  453.     if( $loc_fname ne '-' ){
  454.         # Find the size of the target file
  455.         local( $restart_at ) = &ftp'filesize( $loc_fname );
  456.         if( $restart && $restart_at > 0 && &ftp'restart( $restart_at ) ){
  457.             $restart = 1;
  458.             # Make sure the file can be updated
  459.             chmod( 0644, $loc_fname );
  460.         }
  461.         else {
  462.             $restart = 0;
  463.             unlink( $loc_fname );
  464.         }
  465.     }
  466.  
  467.     &ftp'send( "RETR $rem_fname" );
  468.     
  469.     local( $ret ) =
  470.         &ftp'expect($timeout, 
  471.                    150, "receiving $rem_fname", 1,
  472.  
  473.                    125, "data connection already open?", 0,
  474.  
  475.                    450, "file unavailable", 2,
  476.                    550, "file unavailable", 2,
  477.  
  478.            500, "syntax error", 0,
  479.            501, "syntax error", 0,
  480.            530, "not logged in", 0,
  481.  
  482.            421, "service unavailable, closing connection", 0);
  483.     if( $ret != 1 ){
  484.         print STDERR "Failure on RETR command¥n";
  485.  
  486.         # shut down our end of the socket
  487.         &ftp'close_data_socket;
  488.  
  489.         return 0;
  490.     }
  491.  
  492.     # 
  493.     # the data should be coming at us now
  494.     #
  495.  
  496.     # now accept
  497.     accept(NS,S) || die "accept failed: $!";
  498.  
  499.     #
  500.     #  open the local fname
  501.     #  concatenate on the end if restarting, else just overwrite
  502.     if( !open(FH, ($restart ? '>>' : '>') . $loc_fname) ){
  503.         print STDERR "Cannot create local file $loc_fname¥n";
  504.  
  505.         # shut down our end of the socket
  506.         &ftp'close_data_socket;
  507.  
  508.         return 0;
  509.     }
  510.  
  511. #    while (<NS>) {
  512. #        print FH ;
  513. #    }
  514.  
  515.     local( $start_time ) = time;
  516.     local( $bytes, $lasthash, $hashes ) = (0, 0, 0);
  517.     while( ($len = &ftp'read()) > 0 ){
  518.         $bytes += $len;
  519.         if( $strip_cr ){
  520.             $ftp'buf =~ s/¥r//g;
  521.         }
  522.         if( $ftp_show ){
  523.             while( $bytes > ($lasthash + $ftp'hashevery) ){
  524.                 print STDERR '#';
  525.                 $lasthash += $ftp'hashevery;
  526.                 $hashes++;
  527.                 if( ($hashes % $ftp'hashnl) == 0 ){
  528.                     print STDERR "¥n";
  529.                 }
  530.             }
  531.         }
  532.         if( ! print FH $ftp'buf ){
  533.             print STDERR "¥nfailed to write data";
  534.             return 0;
  535.         }
  536.     }
  537.     close( FH );
  538.  
  539.     # shut down our end of the socket
  540.     &ftp'close_data_socket;
  541.  
  542.     if( $len < 0 ){
  543.         print STDERR "¥ntimed out reading data!¥n";
  544.  
  545.         return 0;
  546.     }
  547.         
  548.     if( $ftp_show ){
  549.         if( $hashes && ($hashes % $ftp'hashnl) != 0 ){
  550.             print STDERR "¥n";
  551.         }
  552.         local( $secs ) = (time - $start_time);
  553.         if( $secs <= 0 ){
  554.             $secs = 1; # To avoid a divide by zero;
  555.         }
  556.  
  557.         local( $rate ) = int( $bytes / $secs );
  558.         print STDERR "Got $bytes bytes ($rate bytes/sec)¥n";
  559.     }
  560.  
  561.     #
  562.     # read the close
  563.     #
  564.  
  565.     $ret = &ftp'expect($timeout, 
  566.         226, "Got file", 1,     # transfer complete, closing connection
  567.             250, "Got file", 1,     # action completed
  568.     
  569.             110, "restart not supported", 0,
  570.             425, "can't open data connection", 0,
  571.             426, "connection closed, transfer aborted", 0,
  572.             451, "action aborted, local error", 0,
  573.         421, "service unavailable, closing connection", 0);
  574.  
  575.     return $ret;
  576. }
  577.  
  578. sub ftp'delete
  579. {
  580.     local( $rem_fname, $val ) = @_;
  581.  
  582.     &ftp'send("DELE $rem_fname" );
  583.     $val = &ftp'expect( $timeout, 
  584.                250,"Deleted $rem_fname", 1,
  585.                550,"Permission denied",0
  586.                );
  587.     return $val == 1;
  588. }
  589.  
  590. sub ftp'deldir
  591. {
  592.     local( $fname ) = @_;
  593.  
  594.     # not yet implemented
  595.     # RMD
  596. }
  597.  
  598. # UPDATE ME!!!!!!
  599. # Add in the hash printing and newline conversion
  600. sub ftp'put
  601. {
  602.     local( $loc_fname, $rem_fname ) = @_;
  603.     local( $strip_cr );
  604.     
  605.     if ($loc_fname eq "") {
  606.         $loc_fname = $rem_fname;
  607.     }
  608.     
  609.     if( ! &ftp'open_data_socket() ){
  610.         return 0;
  611.     }
  612.     
  613.     &ftp'send("STOR $rem_fname");
  614.     
  615.     # 
  616.     # the data should be coming at us now
  617.     #
  618.     
  619.     local( $ret ) =
  620.     &ftp'expect($timeout, 
  621.         150, "sending $loc_fname", 1,
  622.  
  623.         125, "data connection already open?", 0,
  624.         450, "file unavailable", 0,
  625.  
  626.         532, "need account for storing files", 0,
  627.         452, "insufficient storage on system", 0,
  628.         553, "file name not allowed", 0,
  629.  
  630.         500, "syntax error", 0,
  631.         501, "syntax error", 0,
  632.         530, "not logged in", 0,
  633.  
  634.         421, "service unavailable, closing connection", 0);
  635.  
  636.     if( $ret != 1 ){
  637.         # shut down our end of the socket
  638.         &ftp'close_data_socket;
  639.  
  640.         return 0;
  641.     }
  642.  
  643.  
  644.     # 
  645.     # the data should be coming at us now
  646.     #
  647.     
  648.     # now accept
  649.     accept(NS,S) || die "accept failed: $!";
  650.     
  651.     #
  652.     #  open the local fname
  653.     #
  654.     if( !open(FH, "<$loc_fname") ){
  655.         print STDERR "Cannot open local file $loc_fname¥n";
  656.  
  657.         # shut down our end of the socket
  658.         &ftp'close_data_socket;
  659.  
  660.         return 0;
  661.     }
  662.     
  663.     while (<FH>) {
  664.         print NS ;
  665.     }
  666.     close(FH);
  667.     
  668.     # shut down our end of the socket to signal EOF
  669.     &ftp'close_data_socket;
  670.     
  671.     #
  672.     # read the close
  673.     #
  674.     
  675.     $ret = &ftp'expect($timeout, 
  676.         226, "file put", 1,     # transfer complete, closing connection
  677.         250, "file put", 1,     # action completed
  678.     
  679.         110, "restart not supported", 0,
  680.         425, "can't open data connection", 0,
  681.         426, "connection closed, transfer aborted", 0,
  682.         451, "action aborted, local error", 0,
  683.         551, "page type unknown", 0,
  684.         552, "storage allocation exceeded", 0,
  685.     
  686.         421, "service unavailable, closing connection", 0);
  687.     if( ! $ret ){
  688.         print STDERR "error putting $loc_fname¥n";
  689.     }
  690.     return $ret;
  691. }
  692.  
  693. sub ftp'restart
  694. {
  695.     local( $restart_point, $ret ) = @_;
  696.  
  697.     &ftp'send("REST $restart_point");
  698.  
  699.     # 
  700.     # see what they say
  701.  
  702.     $ret = &ftp'expect($timeout, 
  703.                350, "restarting at $restart_point", 1,
  704.                
  705.                500, "syntax error", 0,
  706.                501, "syntax error", 0,
  707.                502, "REST not implemented", 2,
  708.                530, "not logged in", 0,
  709.                554, "REST not implemented", 2,
  710.                
  711.                421, "service unavailable, closing connection", 0);
  712.     return $ret;
  713. }
  714.  
  715. # Set the file transfer type
  716. sub ftp'type
  717. {
  718.     local( $type ) = @_;
  719.  
  720.     &ftp'send("TYPE $type");
  721.  
  722.     # 
  723.     # see what they say
  724.  
  725.     $ret = &ftp'expect($timeout, 
  726.                200, "file type set to $type", 1,
  727.                
  728.                500, "syntax error", 0,
  729.                501, "syntax error", 0,
  730.                504, "Invalid form or byte size for type $type", 0,
  731.                
  732.                421, "service unavailable, closing connection", 0);
  733.     return $ret;
  734. }
  735.  
  736. $site_command_check = 0;
  737. @site_command_list = ();
  738.  
  739. # routine to query the remote server for 'SITE' commands supported
  740. sub ftp'site_commands
  741. {
  742.     local( $ret );
  743.     
  744.     # if we havent sent a 'HELP SITE', send it now
  745.     if( !$site_command_check ){
  746.     
  747.         $site_command_check = 1;
  748.     
  749.         &ftp'send( "HELP SITE" );
  750.     
  751.         # assume the line in the HELP SITE response with the 'HELP'
  752.         # command is the one for us
  753.         $ret = &ftp'expect( $timeout,
  754.             ".*HELP.*", "", "¥$1",
  755.             214, "", "0",
  756.             202, "", "0" );
  757.     
  758.         if( $ret eq "0" ){
  759.             print STDERR "No response from HELP SITE¥n" if( $ftp_show );
  760.         }
  761.     
  762.         @site_command_list = split(/¥s+/, $ret);
  763.     }
  764.     
  765.     return @site_command_list;
  766. }
  767.  
  768. # return the pwd, or null if we can't get the pwd
  769. sub ftp'pwd
  770. {
  771.     local( $ret, $cwd );
  772.  
  773.     &ftp'send( "PWD" );
  774.  
  775.     # 
  776.     # see what they say
  777.  
  778.     $ret = &ftp'expect( $timeout, 
  779.                257, "working dir is", 1,
  780.                500, "syntax error", 0,
  781.                501, "syntax error", 0,
  782.                502, "PWD not implemented", 0,
  783.                        550, "file unavailable", 0,
  784.  
  785.                421, "service unavailable, closing connection", 0 );
  786.     if( $ret ){
  787.         if( $ftp'response =~ /^257¥s"(.*)"¥s.*$/ ){
  788.             $cwd = $1;
  789.         }
  790.     }
  791.     return $cwd;
  792. }
  793.  
  794. # return 1 for success, 0 for failure
  795. sub ftp'mkdir
  796. {
  797.     local( $path ) = @_;
  798.     local( $ret );
  799.  
  800.     &ftp'send( "MKD $path" );
  801.  
  802.     # 
  803.     # see what they say
  804.  
  805.     $ret = &ftp'expect( $timeout, 
  806.                257, "made directory $path", 1,
  807.                
  808.                500, "syntax error", 0,
  809.                501, "syntax error", 0,
  810.                502, "MKD not implemented", 0,
  811.                530, "not logged in", 0,
  812.                        550, "file unavailable", 0,
  813.  
  814.                421, "service unavailable, closing connection", 0 );
  815.     return $ret;
  816. }
  817.  
  818. # return 1 for success, 0 for failure
  819. sub ftp'chmod
  820. {
  821.     local( $path, $mode ) = @_;
  822.     local( $ret );
  823.  
  824.     &ftp'send( sprintf( "SITE CHMOD %o $path", $mode ) );
  825.  
  826.     # 
  827.     # see what they say
  828.  
  829.     $ret = &ftp'expect( $timeout, 
  830.                200, "chmod $mode $path succeeded", 1,
  831.                
  832.                500, "syntax error", 0,
  833.                501, "syntax error", 0,
  834.                502, "CHMOD not implemented", 0,
  835.                530, "not logged in", 0,
  836.                        550, "file unavailable", 0,
  837.  
  838.                421, "service unavailable, closing connection", 0 );
  839.     return $ret;
  840. }
  841.  
  842. # rename a file
  843. sub ftp'rename
  844. {
  845.     local( $old_name, $new_name ) = @_;
  846.     local( $ret );
  847.  
  848.     &ftp'send( "RNFR $old_name" );
  849.  
  850.     # 
  851.     # see what they say
  852.  
  853.     $ret = &ftp'expect( $timeout, 
  854.                350, "", 1,
  855.                
  856.                500, "syntax error", 0,
  857.                501, "syntax error", 0,
  858.                502, "RNFR not implemented", 0,
  859.                530, "not logged in", 0,
  860.                        550, "file unavailable", 0,
  861.                        450, "file unavailable", 0,
  862.                
  863.                421, "service unavailable, closing connection", 0);
  864.  
  865.  
  866.     # check if the "rename from" occurred ok
  867.     if( $ret ) {
  868.         &ftp'send( "RNTO $new_name" );
  869.     
  870.         # 
  871.         # see what they say
  872.     
  873.         $ret = &ftp'expect( $timeout, 
  874.                        250, "rename $old_name to $new_name", 1, 
  875.  
  876.                    500, "syntax error", 0,
  877.                    501, "syntax error", 0,
  878.                    502, "RNTO not implemented", 0,
  879.                    503, "bad sequence of commands", 0,
  880.                    530, "not logged in", 0,
  881.                            532, "need account for storing files", 0,
  882.                            553, "file name not allowed", 0,
  883.                    
  884.                    421, "service unavailable, closing connection", 0);
  885.     }
  886.  
  887.     return $ret;
  888. }
  889.  
  890.  
  891. sub ftp'quote
  892. {
  893.       local( $cmd ) = @_;
  894.  
  895.       &ftp'send( $cmd );
  896.  
  897.       return &ftp'expect( $timeout, 
  898.               200, "Remote '$cmd' OK", 1,
  899.               500, "error in remote '$cmd'", 0 );
  900. }
  901.  
  902. # ------------------------------------------------------------------------------
  903. # These are the lower level support routines
  904.  
  905. sub ftp'expectgot
  906. {
  907.     ($ftp'response, $ftp'fatalerror) = @_;
  908.     if( $ftp_show ){
  909.         print STDERR "$ftp'response¥n";
  910.     }
  911. }
  912.  
  913. #
  914. #  create the list of parameters for chat'expect
  915. #
  916. #  ftp'expect(time_out, {value, string_to_print, return value});
  917. #     if the string_to_print is "" then nothing is printed
  918. #  the last response is stored in $ftp'response
  919. #
  920. # NOTE: lmjm has changed this code such that the string_to_print is
  921. # ignored and the string sent back from the remote system is printed
  922. # instead.
  923. #
  924. sub ftp'expect {
  925.     local( $ret );
  926.     local( $time_out );
  927.     local( $expect_args );
  928.     
  929.     $ftp'response = '';
  930.     $ftp'fatalerror = 0;
  931.  
  932.     @expect_args = ();
  933.     
  934.     $time_out = shift(@_);
  935.     
  936.     while( @_ ){
  937.         local( $code ) = shift( @_ );
  938.         local( $pre ) = '^';
  939.         if( $code =~ /^¥d/ ){
  940.             $pre =~ "[.|¥n]*^";
  941.         }
  942.         push( @expect_args, "$pre(" . $code . " .*)¥¥015¥¥n" );
  943.         shift( @_ );
  944.         push( @expect_args, 
  945.             "&ftp'expectgot( ¥$1, 0 ); " . shift( @_ ) );
  946.     }
  947.     
  948.     # Treat all unrecognised lines as continuations
  949.     push( @expect_args, "^(.*)¥¥015¥¥n" );
  950.     push( @expect_args, "&ftp'expectgot( ¥$1, 0 ); 100" );
  951.     
  952.     # add patterns TIMEOUT and EOF
  953.     
  954.     push( @expect_args, 'TIMEOUT' );
  955.     push( @expect_args, "&ftp'expectgot( ¥"timed out¥", 1 ); 0" );
  956.     
  957.     push( @expect_args, 'EOF' );
  958.     push( @expect_args, "&ftp'expectgot( ¥"remote server gone away¥", 1 ); 0" );
  959.     
  960.     if( $ftp_show > 9 ){
  961.         &printargs( $time_out, @expect_args );
  962.     }
  963.     
  964.     $ret = &chat'expect( $time_out, @expect_args );
  965.     if( $ret == 100 ){
  966.         # we saw a continuation line, wait for the end
  967.         push( @expect_args, "^.*¥n" );
  968.         push( @expect_args, "100" );
  969.     
  970.         while( $ret == 100 ){
  971.             $ret = &chat'expect( $time_out, @expect_args );
  972.         }
  973.     }
  974.     
  975.     return $ret;
  976. }
  977.  
  978. #
  979. #  opens NS for io
  980. #
  981. sub ftp'open_data_socket
  982. {
  983.     local( $ret );
  984.     local( $hostname );
  985.     local( $sockaddr, $name, $aliases, $proto, $port );
  986.     local( $type, $len, $thisaddr, $myaddr, $a, $b, $c, $d );
  987.     local( $mysockaddr, $family, $hi, $lo );
  988.     
  989.     
  990.     $sockaddr = 'S n a4 x8';
  991.     chop( $hostname = `hostname` );
  992.     
  993.     $port = "ftp";
  994.     
  995.     ($name, $aliases, $proto) = getprotobyname( 'tcp' );
  996.     ($name, $aliases, $port) = getservbyname( $port, 'tcp' );
  997.     
  998. #    ($name, $aliases, $type, $len, $thisaddr) =
  999. #    gethostbyname( $hostname );
  1000.     ($a,$b,$c,$d) = unpack( 'C4', $chat'thisaddr );
  1001.     
  1002. #    $this = pack( $sockaddr, &main'AF_INET, 0, $thisaddr );
  1003.     $this = $chat'thisproc;
  1004.     
  1005.     socket(S, $pf_inet, $sock_stream, $proto ) || die "socket: $!";
  1006.     bind(S, $this) || die "bind: $!";
  1007.     
  1008.     # get the port number
  1009.     $mysockaddr = getsockname(S);
  1010.     ($family, $port, $myaddr) = unpack( $sockaddr, $mysockaddr );
  1011.     
  1012.     $hi = ($port >> 8) & 0x00ff;
  1013.     $lo = $port & 0x00ff;
  1014.     
  1015.     #
  1016.     # we MUST do a listen before sending the port otherwise
  1017.     # the PORT may fail
  1018.     #
  1019.     listen( S, 5 ) || die "listen";
  1020.     
  1021.     &ftp'send( "PORT $a,$b,$c,$d,$hi,$lo" );
  1022.     
  1023.     return &ftp'expect($timeout,
  1024.         200, "PORT command successful", 1,
  1025.         250, "PORT command successful", 1 ,
  1026.  
  1027.         500, "syntax error", 0,
  1028.         501, "syntax error", 0,
  1029.         530, "not logged in", 0,
  1030.  
  1031.         421, "service unavailable, closing connection", 0);
  1032. }
  1033.     
  1034. sub ftp'close_data_socket
  1035. {
  1036.     close(NS);
  1037. }
  1038.  
  1039. sub ftp'send
  1040. {
  1041.     local($send_cmd) = @_;
  1042.     if( $send_cmd =~ /¥n/ ){
  1043.         print STDERR "ERROR, ¥¥n in send string for $send_cmd¥n";
  1044.     }
  1045.     
  1046.     if( $ftp_show ){
  1047.         local( $sc ) = $send_cmd;
  1048.  
  1049.         if( $send_cmd =~ /^PASS/){
  1050.             $sc = "PASS <somestring>";
  1051.         }
  1052.         print STDERR "---> $sc¥n";
  1053.     }
  1054.     
  1055.     &chat'print( "$send_cmd¥r¥n" );
  1056. }
  1057.  
  1058. sub ftp'printargs
  1059. {
  1060.     while( @_ ){
  1061.         print STDERR shift( @_ ) . "¥n";
  1062.     }
  1063. }
  1064.  
  1065. sub ftp'filesize
  1066. {
  1067.     local( $fname ) = @_;
  1068.  
  1069.     if( ! -f $fname ){
  1070.         return -1;
  1071.     }
  1072.  
  1073.     return (stat( _ ))[ 7 ];
  1074.     
  1075. }
  1076.  
  1077. # make this package return true
  1078. 1;
  1079.